﻿using System;
using System.Configuration;
using System.Web;
using Game.Utilities.Security;

//using PetRacing.
namespace Game.Utilities.Config
{
    public sealed class Config
    {
        private static readonly Config instance = new Config();

        public static string VTCDBConnectionString { get { return GetConnStr("VTCDBConnectionString"); } }

        public static string GameVietDBConnectionString { get { return GetConnStr("GameVietDBConnectionString"); } }

        public static string GetConnStr(string name)
        {
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;
            //return ConfigurationManager.ConnectionStrings[name]. == null ? "" :
            //new RijndaelEnhanced("pay", "@1B2c3D4e5F6g7H8").Decrypt(ConfigurationManager.ConnectionStrings[name].ConnectionString);
        }

        public static Config Instance
        {
            get { return instance; }
        }

        public static string WebResourceUrl
        {
            get
            {
                return ConfigurationManager.AppSettings["WEB_RESOURCE_URL"].ToString();
            }
        }

        public static string ApplicationUrl
        {
            get
            {
                var url = ConfigurationManager.AppSettings["DomainName"] ?? "http://" + System.Web.HttpContext.Current.Request.Url.Authority +
                   System.Web.HttpContext.Current.Request.ApplicationPath;
                return url.EndsWith("/") ? url : url + "/";
            }
        }

     
        public static string GetIp()
        {
            var ip = string.Empty;

            try
            {
                if (HttpContext.Current.Request.ServerVariables["HTTP_CITRIX"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_CITRIX"];
                }

                if (string.IsNullOrEmpty(ip) && HttpContext.Current.Request.Headers["CITRIX_CLIENT_HEADER"] != null)
                {
                    ip = HttpContext.Current.Request.Headers["CITRIX_CLIENT_HEADER"];
                }

                if (string.IsNullOrEmpty(ip) && HttpContext.Current.Request.Headers["X-Forwarded-For"] != null)
                {
                    return HttpContext.Current.Request.Headers["X-Forwarded-For"];
                }

                if (string.IsNullOrEmpty(ip))
                {
                    if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                    {
                        ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    }
                    if (ip == "")
                    {
                        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }
                }
            }
            catch (Exception ex)
            {
                return string.Empty;
            }

            return ip.Replace('|', '#').Trim();
        }
    }

    
}
